package com.jr.controller;

import com.alibaba.fastjson.JSON;
import com.jr.entity.InvoicingRecord;
import com.jr.service.InvoicingRecordService;
import com.jr.service.OrderService;
import com.jr.service.impl.InvoicingRecordServiceImpl;
import com.jr.service.impl.OrderServiceImpl;
import com.jr.service.impl.UplinkService;
import com.jr.util.Message;
import com.jr.util.ResultUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;

@WebServlet("*.in")
@MultipartConfig
public class InvoiceController extends HttpServlet {
    private InvoicingRecordService invoicingRecordService =new InvoicingRecordServiceImpl();
    private OrderService orderService = new OrderServiceImpl();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 1.获取servletPath /pages/admin/addVip.do
        String servletPath = request.getServletPath();
        // 2.获取到addVip
        String methodName = servletPath.substring(servletPath.lastIndexOf("/") + 1, servletPath.length() - 3);

        try {
            // 3.利用反射获取对应的方法对象
            Method method = getClass().getDeclaredMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
            // 4.执行对应的方法
            method.invoke(this, request, response);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }
    private void addInvoice(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        int enterpriseId = (int) request.getSession().getAttribute("enterpriseId");
        double amount = Double.parseDouble(request.getParameter("amount"));
        int creatorId = (int) request.getSession().getAttribute("creatorId");
        int category = Integer.parseInt(request.getParameter("category"));
        int type = Integer.parseInt(request.getParameter("type"));
        String title = request.getParameter("title");
        String address = request.getParameter("address");
        String email = request.getParameter("email");
        String uplinkAddress = UplinkService.getUplink();
        String no = request.getParameter("no");
        InvoicingRecord invoicingRecord = new InvoicingRecord(amount,enterpriseId,creatorId,category,type,title,address,email,uplinkAddress);
        invoicingRecord.setStatus("已开票");
        try {
            if((!"".equals(email) || !"".equals(address) ) & !"".equals(no)){
                invoicingRecordService.addInvoice(invoicingRecord);
                orderService.updateStatus(no);
                orderService.update(no);
                orderService.delete(no);
                invoicingRecordService.deleteInvoice();
                out.write(JSON.toJSONString(new Message(700, "可以开发票!")));
            }else {
                out.write(JSON.toJSONString(new Message(701, "当前发票不可开!")));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 展示发票信息的请求处理
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void allInvoiceMessage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        try {
            List<InvoicingRecord> allInvoiceMessage = invoicingRecordService.allInvoiceMessage((Integer) request.getSession().getAttribute("enterpriseId"));
            out.write(JSON.toJSONString(new ResultUtil("allInvoice",allInvoiceMessage)));
            //System.out.println(allInvoiceMessage);
            out.flush();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }finally {
            out.close();
        }
    }

    /**
     * 退票请求
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void refundTicket(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        Integer invoiceId= Integer.valueOf(request.getParameter("invoiceId"));
        try {
            int ticket = invoicingRecordService.refundTicket(invoiceId);
            int i = orderService.refundTicket(invoiceId);
            if((ticket>0)&&(i>0)){
                out.write(JSON.toJSONString(new Message(730,"退票成功")));
            }else{
                out.write(JSON.toJSONString(new Message(731,"退票失败")));
            }
            out.flush();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }finally {
            out.close();
        }
    }


    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }
}
